package com.example.mall.web.controller;

import com.example.mall.domain.ProductInventory;
import com.example.mall.dto.ProductInventoryDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ProductInventoryQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IProductInventoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productInventory")
public class ProductInventoryController {

    @Autowired
    public IProductInventoryService productInventoryService;

    /**
     * 分页查询商品库存表信息
     * @param productInventoryQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询商品库存表信息",notes = "分页查询商品库存表信息",httpMethod = "GET")
    public JSONResult queryPage(ProductInventoryQuery productInventoryQuery){
        PageDTO<ProductInventoryDto> productInventoryDtoPageDTO = productInventoryService.queryPage(productInventoryQuery);
        return JSONResult.success("查询成功",productInventoryDtoPageDTO);
    }

    /**
     * 根据id查询 商品库存表信息
     * @param inventoryId
     * @return
     */
    @GetMapping("query/{inventoryId}")
    @ApiOperation(value = "根据id查询商品库存表信息",notes = "根据id查询商品库存表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("inventoryId")Long inventoryId){
        //1.根据id查询用户信息
        if (inventoryId == null){
            throw new GlobalCustomerException(ErrorEnumCode.INVENTORYID_NOT_ID);
        }
        ProductInventory productInventory = productInventoryService.getById(inventoryId);
        return JSONResult.success("查询成功",productInventory);
    }

    /**
     * 根据id查询商品库存表信息
     * @param inventoryId
     * @return
     */
    @GetMapping("queryProductInventoryById/{inventoryId}")
    @ApiOperation(value = "根据id查询商品库存表信息",notes = "根据id查询商品库存表信息",httpMethod = "GET")
    public JSONResult queryProductInventoryById(@PathVariable("inventoryId")Long inventoryId){
        //1.根据id查询用户信息
        if (inventoryId == null){
            throw new GlobalCustomerException(ErrorEnumCode.INVENTORYID_NOT_ID);
        }
        ProductInventory productInventory = productInventoryService.queryProductInventoryById(inventoryId);
        return JSONResult.success("查询成功",productInventory);
    }

    /**
     * 查询全部商品库存表信息
     * @param
     * @return
     */
    @GetMapping("queryProductInventoryList")
    @ApiOperation(value = "查询全部商品库存表信息",notes = "查询全部商品库存表信息",httpMethod = "GET")
    public JSONResult queryProductInventoryList(){
        List<ProductInventory> productInventories = productInventoryService.queryProductInventoryList();
        return JSONResult.success("查询成功",productInventories);
    }

    /**
     * 新增商品库存表
     * @param productInventoryDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增商品库存表",notes = "新增商品库存表",httpMethod = "POST")
    public JSONResult insert(@RequestBody ProductInventoryDto productInventoryDto){
        productInventoryService.insert(productInventoryDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改商品库存表信息
     * @param productInventoryDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改商品库存表信息",notes = "修改商品库存表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody ProductInventoryDto productInventoryDto){
        productInventoryService.update(productInventoryDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除商品库存表信息
     * @param inventoryId
     * @return
     */
    @PostMapping("delete/{inventoryId}")
    @ApiOperation(value = "删除商品库存表信息",notes = "删除商品库存表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("inventoryId") Long inventoryId) {
        productInventoryService.delete(inventoryId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除商品库存表信息
     * @param inventoryIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除商品库存表信息",notes = "批量删除商品库存表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> inventoryIds) {
        productInventoryService.deleteBatch(inventoryIds);
        return JSONResult.success("批量删除成功");
    }
}
